<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>属性列表</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-select style="margin-bottom: 15px" v-model="templateId" placeholder="请选择" @change="attributeList">
      <el-option
          v-for="item in noticeListOptions"
          :label="item.name"
          :value="item.id"
          >
      </el-option>
    </el-select>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="100px" align="center"></el-table-column>
      <el-table-column prop="name" label="名称" width="300" align="center" header-align="center"></el-table-column>
      <el-table-column prop="description" label="简介" align="center" header-align="center"></el-table-column>
      <el-table-column prop="type" label="类型" align="center" header-align="center"></el-table-column>
      <el-table-column prop="inputType" label="输入类型" width="100px" align="center" header-align="center"></el-table-column>
      <el-table-column prop="valueList" label="备选值列表" width="100px" align="center" header-align="center"></el-table-column>
      <el-table-column prop="unit" label="计量单位" width="100px" align="center" header-align="center"></el-table-column>
      <el-table-column prop="sort" label="排序序号" width="100px" align="center" header-align="center"></el-table-column>
      <el-table-column label="操作" width="200" align="center">
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" size="mini"
                     @click="openEditDialog(scope.row)" circle>编辑
          </el-button>
          <el-button type="danger" icon="el-icon-delete" size="mini"
                     @click="confirmDelete(scope.row)" circle>删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--  弹出编辑属性的弹框  -->
    <el-dialog title="修改属性" :visible.sync="dialogFormVisible">
      <el-form :model="editForm" ref="editForm" label-width="130px" class="demo-ruleForm">

        <el-form-item label="属性模板">
          <el-select v-model="editForm.templateId" placeholder="请选择">
            <el-option
                v-for="item in noticeListOptions"
                :label="item.name"
                :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

        <el-form-item label="名称" prop="name">
          <el-input v-model="editForm.name"></el-input>
        </el-form-item>

        <el-form-item label="简介" prop="description">
          <el-input v-model="editForm.description"></el-input>
        </el-form-item>

        <el-form-item label="类型" prop="type">
          <el-radio v-model="editForm.type" label="0">非销售属性</el-radio>
          <el-radio v-model="editForm.type" label="1">销售属性</el-radio>
        </el-form-item>

        <el-form-item label="输入类型" prop="inputType">
          <el-radio v-model="editForm.inputType" label="0">手动录入</el-radio>
          <el-radio v-model="editForm.inputType" label="1">单选</el-radio>
          <el-radio v-model="editForm.inputType" label="2">多选</el-radio>
          <el-radio v-model="editForm.inputType" label="3">单选（下拉列表）</el-radio>
          <el-radio v-model="editForm.inputType" label="4">多选（下拉列表）</el-radio>
        </el-form-item>

        <el-form-item label="备选值列表" prop="valueList">
          <el-input v-model="editForm.valueList"></el-input>
        </el-form-item>

        <el-form-item label="计量单位" prop="unit">
          <el-input v-model="editForm.unit"></el-input>
        </el-form-item>

        <el-form-item label="排位序号" prop="sort">
          <el-input v-model="editForm.sort"></el-input>
        </el-form-item>

        <el-form-item label="是否允许自定义">
          <el-switch
              v-model="editForm.isAllowCustomize"
              :active-value="1"
              :inactive-value="0"
              active-color="#13ce66"
              inactive-color="#cccccc">
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取消</el-button>
        <el-button @click="submitEditForm()" type="primary">修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      noticeListOptions:[],
      templateId:'',
      tableData: [],
      dialogFormVisible: false,
      editForm: {
        id:'',
        templateId:'',
        name: '属性测试1',
        description: '测试类别大大',
        type: '',
        inputType: '',
        valueList:1,
        unit:'',
        sort:'',
        isAllowCustomize:1
      },
    }
  },
  methods:{
    //选择器的属性模板列表
    loadTemplateList(){
      let jwt = localStorage.getItem('jwt');
      console.log('jwt = ' + jwt);
      let url = 'http://localhost:8080/attributeTemplate/list';
      console.log('url = ' +url);

      this.axios
          .create({'headers':{'Authorization' : jwt}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.noticeListOptions = responseBody.data;
        }
      })
    },

    //列表展示
    attributeList(){
      if (this.templateId!==''){
        let url = 'http://localhost:8080/attribute/templateId='+this.templateId+'/list';
        console.log('url = '+ url);
        this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
            .get(url).then((response)=>{
          let responseBody = response.data;
          if (responseBody.state == 20000){
            this.tableData = responseBody.data;
          }
        })
      }
    },

    //弹出删除确认框
    confirmDelete(attribute) {
      console.log('尝试打开删除[id='+attribute.id+']数据的对话框');
      let message = '此操作将永久删除【'+attribute.name+'】属性，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(attribute);
      }).catch(() => {
      });
    },

    //执行删除
    handleDelete(attribute){
      let url = 'http://localhost:8080/attribute/'+attribute.id+'/delete';
      this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state == 20000){
          this.$message.success('删除成功！');
          this.attributeList();
        }else if (responseBody.state==40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.attributeList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      })
    },

    //弹出编辑框
    openEditDialog(attribute){
      console.log('尝试打开编辑【id='+attribute.id+'】数据的对话框');
      let url = 'http://localhost:8080/attribute/'+attribute.id+'/select';
      this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state==20000){
          this.editForm = responseBody.data;
          this.dialogFormVisible = true;
        }else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.attributeList();
            }
          });
        }
      })
    },

    //提交修改
    submitEditForm(){
      let formData = this.qs.stringify(this.editForm);
      let url = 'http://localhost:8080/attribute/'+this.editForm.id+'/update';
      this.axios.create({'headers':{'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response)=>{
        let responseBody = response.data;
        if (responseBody.state==20000){
          this.$message.success("修改属性成功！");
          this.dialogFormVisible = false;
          this.attributeList();
        }else if (responseBody.state==40400){
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.attributeList();
            }
          });
        }else {
          this.$message.error(responseBody.message);
        }
      })
    }
  },
  mounted() {
    this.attributeList();
    this.loadTemplateList();
  }
}
</script>

<style>

</style>